package com.resou.reader.bookdetail.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.resou.reader.R;
import com.resou.reader.api.entry.TypeBean;
import com.resou.reader.bookdetail.BookDetailActivity;

import java.util.List;

/**
 * 书籍详情中书评
 *
 * @author UQIANTU.
 * @date 17/7/7.
 */

public class BookDetailRecomView extends LinearLayout implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {
    private Context context;
    private RecommendAdapter recommendAdapter;
    private TextView tvUpdate;
    private OnClickListener onClickListener;

    public interface OnClickListener {
        void onClick(View view);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public BookDetailRecomView(Context context) {
        super(context);
        init(context);
    }

    public BookDetailRecomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BookDetailRecomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化View
     *
     * @param context
     */
    private void init(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.view_book_detail_recom, this);
        RecyclerView rvComment = findViewById(R.id.rvRec);
        rvComment.setNestedScrollingEnabled(false);
        tvUpdate = findViewById(R.id.tvUpdate);
        recommendAdapter = new RecommendAdapter(R.layout.view_detail_recommend_item);
        rvComment.setAdapter(recommendAdapter);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 4);
        rvComment.setLayoutManager(layoutManager);
        rvComment.setHasFixedSize(true);
        //绑定事件
        bindEvent();
    }

    /**
     * 绑定事件
     */
    public void bindEvent() {
        tvUpdate.setOnClickListener(this);
        recommendAdapter.setOnItemClickListener(this);
    }

    public void setData(List<TypeBean> typeBeans) {
        if (typeBeans != null)
            recommendAdapter.setNewData(typeBeans);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvUpdate:
                if (onClickListener != null) {
                    onClickListener.onClick(v);
                }
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        TypeBean bean = (TypeBean) adapter.getItem(position);
        if (bean != null) {
            BookDetailActivity.startDetailActivity(context, bean.getId());
        }
    }

    class RecommendAdapter extends BaseQuickAdapter<TypeBean, BaseViewHolder> {

        public RecommendAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, TypeBean item) {
            Log.i(TAG, "convert: " + item.getNovelName());
            helper.setText(R.id.tv_book_name, item.getNovelName())
                    .setText(R.id.tv_author, item.getAuthorName())
                    .setVisible(R.id.tvScore, false);
            Glide.with(context)
                    .applyDefaultRequestOptions(new RequestOptions().placeholder(R.mipmap.ic_place)
                            .error(R.mipmap.ic_place).format(DecodeFormat.PREFER_RGB_565))
                    .load(item.getCoverUrl() + "")
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into((ImageView) helper.getView(R.id.iv_cover));

        }


    }
}
